home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 10.4 KB | 373 lines | [TEXT/MPS ] |
- /*
- File: TupleTests.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
- #if 0
- #ifndef __TUPLETESTS__
- #include "TupleTests.h"
- #endif
-
- #ifndef __BTREETUPLEDATABASE__
- #include "BTreeTupleDatabase.h"
- #endif
-
- #ifndef __HFSTUPLEDATABASE__
- #include "HFSTupleDatabase.h"
- #endif
-
- #ifndef __RAMTUPLEDATABASE__
- #include "RamTupleDatabase.h"
- #endif
-
- #ifndef __STRING__
- #include "String.h"
- #endif
-
- #ifndef __STDLIB__
- #include "StdLib.h"
- #endif
-
- #ifndef __MEMORY__
- #include "Memory.h"
- #endif
-
- #pragma segment TupleTests
-
- /***********************************|****************************************/
-
- void FolderTest ()
- {
- TEST_REPORT(FolderTest);
- TFolderSpec folder ( "folder.test" );
- folder >> chris;
-
- TFolderFileIterator iterator ( folder );
- const TFileSpec* fileSpec;
- unsigned long i = 0;
-
- for ( fileSpec = iterator.FirstFile ();
- fileSpec != nil;
- fileSpec = iterator.NextFile () )
- {
- chris << "\nFile " << ++i << "\n";
- *fileSpec >> chris;
- }
- }
-
- /***********************************|****************************************/
-
- Boolean
- InsertTest ( ATupleDatabase& database, const ATupleKey& key, const ADataItem& data )
- {
- // save precondition state
- Boolean alreadyExists = database.DoesTupleExist ( key );
- unsigned long preCount = database.CountTuples ();
-
- ASSERT_RETURN_ZERO ( database.SetTuple ( key, data ) );
- ASSERT_RETURN_ZERO ( database.DoesTupleExist ( key ) );
- unsigned long postCount = database.CountTuples ();
- ASSERT_RETURN_ZERO ( postCount == ( alreadyExists ? preCount : preCount + 1 ) );
-
- // make sure we can do a direct retrieve
- CDataItem tempData;
- ASSERT_RETURN_ZERO ( database.GetTupleData ( key, tempData ) );
- ASSERT_RETURN_ZERO ( tempData == data );
- unsigned long size = 0;
- ASSERT_RETURN_ZERO ( database.GetTupleDataSize ( key, size ) );
- ASSERT_RETURN_ZERO ( size == data.GetPhysicalLength () );
- ASSERT_RETURN_ZERO ( size == tempData.GetPhysicalLength () );
-
- // make sure we can do an indirect retrieve
- unsigned long index = 0;
- ASSERT_RETURN_ZERO ( database.FindIndexOfTuple ( key, index ) );
- CTupleKey tempKey; tempData.SetPhysicalLength ( 0 );
- ASSERT_RETURN_ZERO ( database.GetTuple ( index, tempKey, tempData ) );
- ASSERT_RETURN_ZERO ( tempKey == key );
- ASSERT_RETURN_ZERO ( tempData == data );
- tempKey.SetLength ( 0 );
- ASSERT_RETURN_ZERO ( database.GetTupleKey ( index, tempKey ) );
- ASSERT_RETURN_ZERO ( tempKey == key );
- tempData.SetPhysicalLength ( 0 );
- ASSERT_RETURN_ZERO ( database.GetTupleData ( index, tempData ) );
- ASSERT_RETURN_ZERO ( tempData == data );
-
- return true;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- class CWriteBuffer : public CDataItem
- {
- public: CWriteBuffer ( const char* );
- };
-
- CWriteBuffer::CWriteBuffer ( const char* s ):
- CDataItem ( s, strlen ( s ) )
- {
- }
-
- /***********************************|****************************************/
-
- void
- TupleDatabaseTest ( ATupleDatabase& database, const ATupleKey& key1, const ATupleKey& key2, const ATupleKey& key3 )
- {
- TEST_REPORT(TupleDatabaseTest);
-
- const CWriteBuffer wrData1 ( "data 1 string which is rather long" );
- const CWriteBuffer wrData2 ( "data 2 string which is even longer than the first" );
- const CWriteBuffer wrData3 ( "data 3 string is shorter" );
- const CWriteBuffer wrData4 ( "new data for key 1" );
- const CWriteBuffer wrData5 ( "key 2 new data" );
- const CWriteBuffer wrData6 ( "data for key 3" );
-
- ASSERT_RETURN ( InsertTest ( database, key1, wrData1 ) );
- ASSERT_RETURN ( InsertTest ( database, key2, wrData2 ) );
- ASSERT_RETURN ( InsertTest ( database, key1, wrData4 ) );
- ASSERT_RETURN ( InsertTest ( database, key3, wrData6 ) );
- ASSERT_RETURN ( InsertTest ( database, key1, wrData1 ) );
- ASSERT_RETURN ( InsertTest ( database, key3, wrData3 ) );
- ASSERT_RETURN ( InsertTest ( database, key2, wrData5 ) );
- ASSERT_RETURN ( InsertTest ( database, key1, wrData4 ) );
- }
-
- /***********************************|****************************************/
-
- void HFSDatabaseTest ()
- {
- TEST_REPORT(HFSDatabaseTest);
- TFolderSpec folder ( "hfs.test" );
- folder.DeleteFolder ();
- THFSTupleDatabase database ( folder );
- const CTupleKey key1 ( "\pkey 1 (one)" ), key2 ( "\pkey 2 (two, two keys in one)" ), key3 ( "\pkey 3 (three, or thrice)" );
- TupleDatabaseTest ( database, key1, key2, key3 ); // run tests with new folder
- TupleDatabaseTest ( database, key1, key2, key3 ); // run tests with existing folder
- }
-
- /***********************************|****************************************/
-
- class CLongShortShortDescriptor : public AKeyDescriptor
- {
- public: CLongShortShortDescriptor ();
- virtual ~CLongShortShortDescriptor ();
-
- virtual const unsigned char* GetBtreeDescriptor () const;
- };
-
- /***********************************|****************************************/
-
- class CLongShortShortKey : public ATupleKey
- {
- public: CLongShortShortKey ( long, short, short );
- virtual ~CLongShortShortKey ();
-
- virtual const void* GetData () const;
- virtual unsigned long GetLength () const;
- virtual unsigned long SetLength ( unsigned long );
-
- virtual long Compare ( const AComparable& tupleKeysOnly ) const;
- virtual ostream& operator >> ( ostream& ) const;
-
- private:
-
- struct Data { long fLong; short fShort1, fShort2; };
-
- Data fData;
- };
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- CLongShortShortDescriptor::CLongShortShortDescriptor ():
- AKeyDescriptor ()
- {
- }
-
- /***********************************|****************************************/
-
- CLongShortShortDescriptor::~CLongShortShortDescriptor ()
- {
- }
-
- /***********************************|****************************************/
-
- const unsigned char*
- CLongShortShortDescriptor::GetBtreeDescriptor () const
- {
- static unsigned char kDescriptor [] = { 4, kdLong, 1, kdWord, 2 };
- return kDescriptor;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- CLongShortShortKey::CLongShortShortKey ( long l, short s1, short s2 ):
- ATupleKey ()
- {
- fData.fLong = l;
- fData.fShort1 = s1;
- fData.fShort2 = s2;
- }
-
- /***********************************|****************************************/
-
- CLongShortShortKey::~CLongShortShortKey ()
- {
- }
-
- /***********************************|****************************************/
-
- const void*
- CLongShortShortKey::GetData () const
- {
- return &fData;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- CLongShortShortKey::GetLength () const
- {
- return sizeof ( fData );
- }
-
- /***********************************|****************************************/
-
- unsigned long
- CLongShortShortKey::SetLength ( unsigned long length )
- {
- return sizeof ( fData );
- }
-
- /***********************************|****************************************/
-
- long
- CLongShortShortKey::Compare ( const AComparable& other ) const
- {
- const CLongShortShortKey& that = (const CLongShortShortKey&) other;
- ASSERT ( GetLength () == that.GetLength () );
- const Data& thatData = *(const Data*) that.GetData ();
-
- return
- fData.fLong == thatData.fLong &&
- fData.fShort1 == thatData.fShort1 &&
- fData.fShort2 == thatData.fShort2;
- }
-
- /***********************************|****************************************/
-
- ostream&
- CLongShortShortKey::operator >> ( ostream& stream ) const
- {
- stream << "CLongShortShortKey @ " << (void*) this << '\n';
- ATupleKey::operator >> ( stream );
- stream << "\tfData.fLong: " << fData.fLong << endl;
- stream << "\tfData.fShort1: " << fData.fShort1 << endl;
- return stream << "\tfData.fShort2: " << fData.fShort2 << endl;
- }
-
- /***********************************|****************************************/
-
- void BTreeDatabaseTest ()
- {
- TEST_REPORT(BTreeDatabaseTest);
-
- #if 0
-
- const CLongShortShortDescriptor kDescriptor;
- const CLongShortShortKey key1 ( 1, 2, 3 );
- const CLongShortShortKey key2 ( 4, 5, 6 );
- const CLongShortShortKey key3 ( 7, 8, 9 );
-
- #else
-
- const AKeyDescriptor& kDescriptor = AKeyDescriptor::kString;
- const CTupleKey key1 ( "\pkey 1 (one)", true );
- const CTupleKey key2 ( "\pkey 2 (two, two keys in one)", true );
- const CTupleKey key3 ( "\pkey 3 (three, or thrice)", true );
-
- #endif
-
- TFileSpec file ( "btree.test" );
- file.DeleteFile ();
- chris << kDescriptor << endl;
- TBTreeDatabase database ( file, kDescriptor, TBTreeDatabase::kReadWriteNew, TBTreeDatabase::kNonExclusive );
- TupleDatabaseTest ( database, key1, key2, key3 ); // once with new file
- TupleDatabaseTest ( database, key1, key2, key3 ); // once with old file
-
- TFileSpec file2 ( "btree2.test" );
- file2.DeleteFile ();
- chris << kDescriptor << endl;
- TBTreeDatabase database2 ( file2, kDescriptor, TBTreeDatabase::kReadWriteNew, TBTreeDatabase::kExclusive );
- TupleDatabaseTest ( database2, key1, key2, key3 ); // once with new file
- TupleDatabaseTest ( database2, key1, key2, key3 ); // once with old file
- }
-
- /***********************************|****************************************/
-
- void RamDatabaseTest ()
- {
- TRamTupleDatabase database;
- const CFourByteKey key1 ( (unsigned long) 235656 ), key2 ((long) -45344 ), key3 ((void*) 0x482efd);
- TupleDatabaseTest ( database, key1, key2, key3 );
- }
-
- /***********************************|****************************************/
-
- void AllDatabaseTests ()
- {
- HFSDatabaseTest ();
- RamDatabaseTest ();
- BTreeDatabaseTest ();
- }
-
- /***********************************|****************************************/
-
- void TupleTests ()
- {
- TEST_REPORT(TupleTests);
- CTupleKey key1 ( "key 1" ), key2 ( "key2" );
- ASSERT ( key1 != key2 );
- key1 = key2;
- ASSERT ( key1 == key2 );
- }
-
- /***********************************|****************************************/
-
- #define PRINT_SIZE(C) chris << "sizeof ( " << #C << ") = " << sizeof ( C ) << '\n'
-
- void
- PrintSizes ()
- {
- PRINT_SIZE ( TFileSpec );
- PRINT_SIZE ( TFolderSpec );
-
- PRINT_SIZE ( ADataItem );
- PRINT_SIZE ( CDataItem );
- PRINT_SIZE ( CPrefixDataItem );
- PRINT_SIZE ( CWrapperBuffer );
-
- PRINT_SIZE ( ATupleKey );
- PRINT_SIZE ( CTupleKey );
- PRINT_SIZE ( CFourByteKey );
-
- PRINT_SIZE ( AKeyDescriptor );
- PRINT_SIZE ( CWrapperBuffer );
-
- PRINT_SIZE ( ATupleDatabase );
- PRINT_SIZE ( TRamTupleDatabase );
- PRINT_SIZE ( THFSTupleDatabase );
- PRINT_SIZE ( TBTreeDatabase );
- }
-
- /***********************************|****************************************/
- #endif
-